package com.woniuxy.cglibproxy;

import com.woniuxy.aspect.MyAspect;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @BelongsProject: java-85-course-code
 * @BelongsPackage: com.woniuxy.cglibproxy
 * @Author: LiTongMiao
 * @Date: 2022/1/20 17:06
 * @Description: TODO
 */
public class CglibProxyFactory {
    public static Object getCglibProxy(Object targetClass) {
        Object o = Enhancer.create(targetClass.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //前置增强
                MyAspect myAspect = new MyAspect();
                myAspect.beforeLog();
                //目标方法
                Object invoke = method.invoke(targetClass, args);
                myAspect.afterLog();
                return invoke;
            }
        });
        return o;

            }
        }
